public class MoveZeroes {
    // 移动零
    // https://leetcode.cn/problems/move-zeroes/?envType=study-plan-v2&envId=top-100-liked
    public void moveZeroes(int[] nums) {
        // 通过 left 指针和 right 指针将整个数组分为三个部分
        // [0,left]已扫描的非零元素 (left, right)已扫描的零元素 [left, length)待扫描的元素
        for (int left = -1, right = 0; right < nums.length; right++) {
            if (nums[right] != 0) {
                swap(nums, ++left, right);
            }
        }
    }

    private void swap(int[] nums, int x, int y) {
        int tmp = nums[x];
        nums[x] = nums[y];
        nums[y] = tmp;
    }
}
